package math;

import java.awt.Color;
import java.util.Scanner;

/**
 * An RGB color element in the form of a vector
 */
public class RGBvec {
	
	public Vec color;
	
	/**
	 * Create an RGB vector using parsed string, useful for XML data
	 * @param textInput, string to be parsed into red, green, and blue numbers
	 */
	public RGBvec(String textInput) {
		Scanner scan = new Scanner(textInput);
		float red = scan.nextFloat();
		float green = scan.nextFloat();
		float blue = scan.nextFloat();
		this.color = new Vec(red, green, blue);
	}
	/**
	 * Create RGB vector with given R G B input
	 * @param red
	 * @param green
	 * @param blue
	 */
	public RGBvec(double red, double green, double blue) {
		this.color = new Vec(red, green, blue);
	}
	
	/**
	 * Create RGB vector from another vector
	 * @param v
	 */
	public RGBvec(Vec v) {
		this.color = new Vec(v.x, v.y, v.z);
	}

	/**
	 * Get color from background
	 * @param background
	 */
	public RGBvec(Color background) {
		this.color = new Vec(background.getRed() / 255d,
				background.getGreen() / 255d,
				background.getBlue() / 255d);
	}
	
	/**
	 * Converts color vector into regular Color object
	 * @return, a Java Color object form of the color vector
	 */
	public Color toJavaColor() {
		this.normalize();
		return new Color(this.red(),this.green(),this.blue());
	}
	
	/**
	 * Add two color vectors
	 * @param rgb
	 */
	public void add(RGBvec vec) {
		Vec addColor = new Vec(vec.red(), vec.green(), vec.blue());
		this.color.add(addColor);
	}
	
	// get red, green, blue values
	public float red()
	{
		return (float)this.color.x;
	}
	
	public float green()
	{
		return (float)this.color.y;
	}
	
	public float blue()
	{
		return (float)this.color.z;
	}
	
	/**
	 * Scale vector
	 * @param d
	 */
	public void mult(double d) {
		this.color.scale(d);		
	}
	
	/**
	 * Normalize the vector
	 */
	public void normalize() {
		this.color.x = (this.color.x < 0) ? 0 : 1;
		this.color.y = (this.color.y < 0) ? 0 : 1;
		this.color.z = (this.color.y < 0) ? 0 : 1;
	}
	
	/**
	 * Convert an RGBvec to regular vector
	 * @param RGBvec
	 * @return, a regular Vector
	 */
	public static Vec toVec(RGBvec vec) {
		return new Vec(vec.red(),vec.green(),vec.blue());
	}
	
}
